<template>
  <div class="upload-container">
    <el-upload
      v-show="imageUrl.length===0"
      :data="{authorization:upyun.authorization,policy:upyun.policy}"
      :multiple="false"
      :show-file-list="false"
      :on-success="handleImageSuccess"
      class="image-uploader"
      drag
      :action="`${upyun.upurl}`"
      :before-upload="beforeUpload"
    >
      <i class="el-icon-upload" />
      <div class="el-upload__text">
        将文件拖到此处，或<em>点击上传</em>
      </div>
    </el-upload>
    <div v-show="imageUrl.length>1" class="image-preview">
      <div class="image-preview-wrapper">
        <img :src="getImage(imageUrl,'/max/500/canvas/500x500/cvscolor/FFFFFFFF')">
        <div class="image-preview-action">
          <i class="el-icon-delete" @click="rmImage" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Upyun from '@/utils/upyun'
const image = ''
export default {
  name: 'SingleImageUpload1',
  props: {
    value: {
      type: String,
      default: image
    },
    remotePath: {
      type: String,
      default: 'upload/{filemd5}{.suffix}'
    }
  },
  data() {
    return {
      tempUrl: '',
      dataObj: { token: '', key: '' },
      upyun: new Upyun()
    }
  },
  computed: {
    imageUrl: {
      get() {
        if (this.value && this.value !== '') {
          return this.value
        }
        return image
      },
      set(value) {
        this.$emit('input', value.join(','))
      }
    }
  },
  created() {
    this.value = this.value || ''
  },
  methods: {
    async beforeUpload(file) {
      await this.upyun.getSignature({ remotePath: this.remotePath })
      return true
    },
    rmImage() {
      this.emitInput('')
    },
    emitInput(val) {
      this.$emit('input', val)
    },
    handleImageSuccess(res, file) {
      this.emitInput(this.upyun.fileurl + res.url)
    }
  }
}
</script>

<style lang="scss" scoped>
    @import "~@/styles/mixin.scss";
    .upload-container {
        width: 100%;
        position: relative;
        @include clearfix;
        .image-uploader {
            width: 60%;
            float: left;
        }
        .image-preview {
            width: 200px;
            height: 200px;
            position: relative;
            border: 1px dashed #d9d9d9;
            float: left;
            .image-preview-wrapper {
                position: relative;
                width: 100%;
                height: 100%;
                img {
                    width: 100%;
                    height: 100%;
                }
            }
            .image-preview-action {
                position: absolute;
                width: 100%;
                height: 100%;
                left: 0;
                top: 0;
                cursor: default;
                text-align: center;
                color: #fff;
                opacity: 0;
                font-size: 20px;
                background-color: rgba(0, 0, 0, .5);
                transition: opacity .3s;
                cursor: pointer;
                text-align: center;
                line-height: 200px;
                .el-icon-delete {
                    font-size: 36px;
                }
            }
            &:hover {
                .image-preview-action {
                    opacity: 1;
                }
            }
        }
    }

</style>
